//
//  FGStatusPictureView.swift
//  飞哥大微博
//
//  Created by huangfei on 2017/6/12.
//  Copyright © 2017年 huangfei. All rights reserved.
//

import UIKit

class FGStatusPictureView: UIView {
    
    // 配图视图的数组
    var urls:[FGStatusPicture]? {
        didSet {
            // 1.隐藏所有的imageView
            for v in subviews {
                v.isHidden = true
            }
            // 2.遍历urls数组，顺序设置图像
            var index = 0
            for url in urls ?? [] {
                // 获得对应索引的 imageView
                let iv = subviews[index] as! UIImageView
                // 设置图像
                iv.fg_setImage(urlString: url.thumbnail_pic, placeholderImage: nil)
                // 显示图像
                iv.isHidden = false
                index += 1
            }
        }
    }
    
    @IBOutlet weak var heightCons:NSLayoutConstraint!
    
    override func awakeFromNib() {
        setupUI()
    }
}

extension FGStatusPictureView {
    
    // 1.cell中所有的控件都提前转备好
    // 2.设置的时候，根据数量决定是否显示
    // 3.不要动态创建控件
    func setupUI() {
        
        // 超出边界的内容不显示
        clipsToBounds = true
        
        let count = 3
        let rect = CGRect(x: 0,
                          y: FGStatusPictureViewOutterMargin,
                          width: FGStatusPictureItemWidth,
                          height: FGStatusPictureItemWidth)
        // 循环创建9个imageView
        for i in 0..<count * count {
            let iv = UIImageView()
            iv.backgroundColor = UIColor.red
            // 行 -> Y
            let row = CGFloat(i / count)
            // 列 -> X
            let col = CGFloat(i % count)
            let xOffset = col * (FGStatusPictureItemWidth + FGStatusPictureViewInnerMargin)
            let yOffset = row * (FGStatusPictureItemWidth + FGStatusPictureViewInnerMargin)
            iv.frame = rect.offsetBy(dx: xOffset, dy: yOffset)
            
            addSubview(iv)
        }
    }
}
